package com.vercon.otamirror.receiver;


import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadset;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class BluetoothReceiverListener {


    private final String TAG = BluetoothReceiverListener.class.getSimpleName();
    private  static  BluetoothReceiverListener mInstance;


    private List<BluetoothStateListener> bluetoothStateListenerList = new ArrayList<>();

    public static BluetoothReceiverListener getInstance() {
        if (mInstance == null) {
            synchronized (BluetoothReceiverListener.class) {
                if (mInstance == null) {
                    mInstance = new BluetoothReceiverListener();
                }
            }
        }
        return mInstance;
    }

    public void setReceiver(Context context){

        IntentFilter mFilter = new IntentFilter();
        mFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);//状态改变
        mFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); //蓝牙开关状态
        mFilter.addAction(BluetoothDevice.ACTION_FOUND);//蓝牙发现新设备(未配对)
        mFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); //蓝牙开始搜索
        mFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); //蓝牙搜索结束
        mFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); //设备配对状态改变
        mFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);//设备建立连接
        mFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED); //设备断开连接
        mFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED); //BluetoothAdapter连接状态
        context.registerReceiver(mBluetoothReceiver,mFilter);

    }

    public void unReceiver(Context context){
        context.unregisterReceiver(mBluetoothReceiver);
    }

    private final BroadcastReceiver mBluetoothReceiver=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            BluetoothDevice dev;
            int state;
            if (action == null) {
                return;
            }

            switch (action) {
                case  BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE:
                    Log.i(TAG, "可被搜索请求");
                    for (BluetoothStateListener listener: bluetoothStateListenerList) {
                        listener.ACTION_REQUEST_DISCOVERABLE(intent);
                    }
                    break;
                /**
                 * 蓝牙开关状态
                 * int STATE_OFF = 10; //蓝牙关闭
                 * int STATE_ON = 12; //蓝牙打开
                 * int STATE_TURNING_OFF = 13; //蓝牙正在关闭
                 * int STATE_TURNING_ON = 11; //蓝牙正在打开
                 */
                case BluetoothAdapter.ACTION_STATE_CHANGED:
                    state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
                    Log.i(TAG, "蓝牙开关状态" + state);
                    for (BluetoothStateListener listener: bluetoothStateListenerList) {
                        listener.ACTION_STATE_CHANGED(intent);
                    }
                    break;
                /**
                 * 蓝牙开始搜索
                 */
                case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
                    Log.i(TAG, "蓝牙开始搜索");
                    for (BluetoothStateListener listener: bluetoothStateListenerList) {
                        listener.ACTION_DISCOVERY_STARTED(intent);
                    }
                    break;
                /**
                 * 蓝牙搜索结束
                 */
                case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
                    Log.i(TAG, "蓝牙扫描结束");
                    for (BluetoothStateListener listener: bluetoothStateListenerList) {
                        listener.ACTION_DISCOVERY_FINISHED(intent);
                    }
                    break;
                /**
                 * 发现新设备
                 */
                case BluetoothDevice.ACTION_FOUND:
                    Log.i(TAG, "发现新设备");
                    for (BluetoothStateListener listener: bluetoothStateListenerList) {
                        listener.ACTION_FOUND(intent);
                    }
                    break;
                /**
                 * 设备配对状态改变
                 * int BOND_NONE = 10; //配对没有成功
                 * int BOND_BONDING = 11; //配对中
                 * int BOND_BONDED = 12; //配对成功
                 */
                case BluetoothDevice.ACTION_BOND_STATE_CHANGED:
                    Log.i(TAG, "设备配对状态改变" );
                    for (BluetoothStateListener listener: bluetoothStateListenerList) {
                        listener.ACTION_BOND_STATE_CHANGED(intent);
                    }
                    break;
                /**
                 * 设备建立连接
                 * int STATE_DISCONNECTED = 0; //未连接
                 * int STATE_CONNECTING = 1; //连接中
                 * int STATE_CONNECTED = 2; //连接成功
                 */
                case BluetoothDevice.ACTION_ACL_CONNECTED:
                    Log.i(TAG, "设备建立连接：");
                    for (BluetoothStateListener listener: bluetoothStateListenerList) {
                        listener.ACTION_ACL_CONNECTED(intent);
                    }
                    break;
                /**
                 * 设备断开连接
                 */
                case BluetoothDevice.ACTION_ACL_DISCONNECTED:
                    Log.i(TAG, "设备断开连接：");
                    for (BluetoothStateListener listener: bluetoothStateListenerList) {
                        listener.ACTION_ACL_DISCONNECTED(intent);
                    }
                    break;
                /**
                 * 提供用于手机的蓝牙耳机支持
                 * BluetoothHeadset连接状态
                 */
                case BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED:
                    Log.i(TAG, "连接状态：");
                    for (BluetoothStateListener listener: bluetoothStateListenerList) {
                        listener.ACTION_CONNECTION_STATE_CHANGED(intent);
                    }
                    break;
                default:
                    break;
            }
        }
    };


    public void addListener(BluetoothStateListener listener){
        bluetoothStateListenerList.add(listener);
    }

    public void removeListener(BluetoothStateListener listener){
        try {
            bluetoothStateListenerList.remove(listener);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public interface BluetoothStateListener{

        //可被搜索请求
        void ACTION_REQUEST_DISCOVERABLE(Intent intent);
        /**
         * 蓝牙开关状态
         * int STATE_OFF = 10; //蓝牙关闭
         * int STATE_ON = 12; //蓝牙打开
         * int STATE_TURNING_OFF = 13; //蓝牙正在关闭
         * int STATE_TURNING_ON = 11; //蓝牙正在打开
         */
        void ACTION_STATE_CHANGED(Intent intent);

        /**
         * 蓝牙开始搜索
         */
        void ACTION_DISCOVERY_STARTED(Intent intent);

        /**
         * 蓝牙搜索结束
         */
        void ACTION_DISCOVERY_FINISHED(Intent intent);

        /**
         * 发现新设备
         */
        void ACTION_FOUND(Intent intent);

        /**
         * 设备配对状态改变
         * int BOND_NONE = 10; //配对没有成功
         * int BOND_BONDING = 11; //配对中
         * int BOND_BONDED = 12; //配对成功
         */
        void ACTION_BOND_STATE_CHANGED(Intent intent);

        /**
         * 设备建立连接
         * int STATE_DISCONNECTED = 0; //未连接
         * int STATE_CONNECTING = 1; //连接中
         * int STATE_CONNECTED = 2; //连接成功
         */
        void ACTION_ACL_CONNECTED(Intent intent);

        /**
         * 设备断开连接
         */
        void ACTION_ACL_DISCONNECTED(Intent intent);
        /**
         * 提供用于手机的蓝牙耳机支持
         * BluetoothHeadset连接状态
         */
        void ACTION_CONNECTION_STATE_CHANGED(Intent intent);
        void Headset_STATE_CONNECTING(Intent intent);//连接中
        void Headset_STATE_CONNECTED(Intent intent);//已连接
        void Headset_STATE_DISCONNECTED(Intent intent);//断开
        void Headset_STATE_DISCONNECTING(Intent intent);//断开中

    }


}
